using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GameForKids
{
    class Action
    {
        public int _Count;
        public int _StartIndex;
    }

    class Sprite
    {
        protected int _index;//vi tri hien tai
        protected int _spriteHeight;//chieu cai cua sprite anh
        protected int _spriteWidth;// chieu rong cua sprite anh
        protected int _column;//so cot cuar anh
        protected int _row;// so dong cua anh
        protected int _count;// tong so sprite

        public int _SpriteHeight { get { return _spriteHeight; } }
        public int _SpriteWidth { get { return _spriteWidth; } }

        protected Vector2 _original;
        public Vector2 _Original { get { return _original; } }

        protected Dictionary<string, Action> _actions;// cac hanh dong
        protected Action _currentAction;// hanh dong hien tai
        protected string _currentActionName;//ten cua hanh dong hien tai

        public double _TimePerSpriteFrame = 1.0f / 30.0f;
        public double _TotalElapedGameTime = 0.0f;

        public float _Scale = 1.0f;

        public float _Rotation = 0.0f;

        public SpriteEffects _SpriteEffects = SpriteEffects.None;
        public Color _Color = new Color(255, 255, 255, 1.0f);
        public float _A = 1.0f;
        public float _R = 255;
        public float _B = 255;
        public float _G = 255;
        public float _LayerDepth = 0;
        /// <summary>
        /// Get the current action link
        /// </summary>
        public string _CurrentActionName{ get { return _currentActionName; }}

        public Sprite(int spriteHeight, int spriteWidth, int column
            , int row, int count, int actionStartIndex
            , int actionCount, string currentActionName
            , float timePerSpriteFrame)

        {
            _spriteHeight = spriteHeight;
            _spriteWidth = spriteWidth;
            _original = new Vector2(_spriteWidth / 2, _spriteHeight / 2);
            _column = column;
            _row = row;
            _count = count;
            _actions = new Dictionary<string, Action>();
            _currentAction = new Action() { _Count = actionCount, _StartIndex = actionStartIndex};
            _currentActionName = currentActionName;
            _actions.Add(currentActionName, _currentAction);
            _index = actionStartIndex;
            _TimePerSpriteFrame = timePerSpriteFrame;
            _Scale = 1.0f;
            _Rotation = 0.0f;
        }

        /// <summary>
        /// Sprite ke tiep trong 1 action
        /// </summary>
        public void Next()
        {
            if (_index + 1 > _currentAction._Count + _currentAction._StartIndex - 1)
            {
                _index = _currentAction._StartIndex;
            }
            else
            {
                _index++;
            }
        }

        /// <summary>
        /// Them 1 action
        /// </summary>
        /// <param link="actionName"></param>
        /// <param link="startIndex"></param>
        /// <param link="countX"></param>
        /// <returns>dung khi them dc</returns>
        public bool AddAction(string actionName, int startIndex, int count)
        {
            if (_actions.ContainsKey(actionName))
                return false;

            _actions.Add(actionName, new Action(){ _Count = count, _StartIndex = startIndex});
            return true;
        }

        /// <summary>
        /// Chuyen doi hanh dong cua Sprite
        /// </summary>
        /// <param link="actionName"></param>
        /// <returns>Dung khi chuyen dc. Sai khi hanh dong khong ton tai</returns>
        public bool ChangeAction(string actionName)
        {
            if (_actions.ContainsKey(actionName) == false)
                return false;

            _currentActionName = actionName;
            _currentAction = _actions[actionName];
            _index = _currentAction._StartIndex;
            return true;
        }

        /// <summary>
        /// Lay rectangle trong vung texture de ve
        /// </summary>
        public Rectangle _SourceRectangle { get { return new Rectangle((_index % _column) * _spriteWidth, (_index / _column) * _spriteHeight, _spriteWidth, _spriteHeight); } }

        public bool IsCollitionWithoutAngle(Vector2 CurrentCenterPosition, Rectangle RecCompare)
        {
            Rectangle rec = new Rectangle((int)(CurrentCenterPosition.X - _original.X * _Scale), (int)(CurrentCenterPosition.Y - _original.Y * _Scale), (int)(_spriteWidth * _Scale), (int)((_spriteHeight * _Scale)));
            return rec.Intersects(RecCompare);
        }


        public void Update(GameTime gameTime) { UpdateSprite(gameTime); }

        public virtual void UpdateSprite(GameTime gameTime) 
        {
            _TotalElapedGameTime += (double)(gameTime.ElapsedGameTime.Milliseconds / 1000.0f);

            if (_TotalElapedGameTime >= _TimePerSpriteFrame)
            {
                Next();
                _TotalElapedGameTime = _TotalElapedGameTime - _TimePerSpriteFrame;
            }
        }

        public Vector2 Position(Vector2 currentCenterPosition)
        {

            return new Vector2(currentCenterPosition.X - (_spriteWidth * _Scale) / 2.0f, currentCenterPosition.Y - (_spriteHeight * _Scale) /2.0f);
        }

        public Rectangle ScreenRectangle(Vector2 currentCenterPosition)
        {
            Vector2 pos = Position(currentCenterPosition);
            return new Rectangle((int)pos.X, (int)pos.Y, (int)(_spriteWidth * _Scale), (int)(_spriteHeight * _Scale));
           
        }

        public Rectangle RealScreenRectangle(Vector2 currentCenterPosition)
        {
            float x = currentCenterPosition.X - (_SpriteWidth * _Scale);
            float y = currentCenterPosition.Y - (_SpriteHeight * _Scale);
            return new Rectangle((int)x, (int)y, (int)(_spriteWidth * _Scale), (int)(_spriteHeight * _Scale));
        
        }
    }
}

